/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package capanegocio;
import java.sql.*;
/**
 *
 * @author Coco
 */
public class bus {
    conectate con;
    public bus(){
        con=new conectate();
    }
      public void newbus(String patente,String rut,String modelo,int ano,int capa){
       try {            
           PreparedStatement pstm = con.getConnection().prepareStatement("insert into "+
                    "bus(Pantente, Chofer_Rut, Modelo, Year, Capacidad) "+
                    "values(?,?,?,?,?)");
            pstm.setString(1, patente);
            pstm.setString(2, rut);
             pstm.setString(3, modelo);
            pstm.setInt(4, ano);
            pstm.setInt(5, capa);                        
              pstm.execute();
            pstm.close();
         }catch(SQLException e){
          System.out.println(e);
      }
   }
      public void updatebus(String patente,String rut,String modelo,int ano,int capa){
       try {            
            PreparedStatement pstm = con.getConnection().prepareStatement("update bus " +
            "set Pantente = ? ," +
            "Chofer_Rut = ? ," +
            "Modelo = ? ," +                    
            "Year = ? " +
            "Capacidad"  +     
            "where Patente = ? ");            
            
            pstm.setString(1, patente);
            pstm.setString(2, rut);
            pstm.setString(3, modelo);
            pstm.setInt(4,ano);                        
            pstm.setInt(5, capa);
            pstm.setString(6, String.valueOf(patente));
            pstm.execute();
            pstm.close();            
         }catch(SQLException e){
         System.out.println(e);
      }
   }
      public void deletebus(String cod){  
            try {                
                PreparedStatement pstm = con.getConnection().prepareStatement("delete from bus where Pantente = ?");            
                pstm.setString(1, cod);                   
                pstm.execute();
                pstm.close();            
            }catch(SQLException e){
            System.out.println(e);
            }            
   }
      public Object [][] getDatos(){
      int registros = 0;
      //obtenemos la cantidad de registros existentes en la tabla
      try{         
         PreparedStatement pstm = con.getConnection().prepareStatement("SELECT count(1) as total FROM bus ");
         ResultSet res = pstm.executeQuery();
         res.next();
         registros = res.getInt("total");
         res.close();
      }catch(SQLException e){
         System.out.println(e);
      }
      
    Object[][] data = new String[registros][5];  
    //realizamos la consulta sql y llenamos los datos en "Object"
      try{    
         PreparedStatement pstm = con.getConnection().prepareStatement("SELECT " +
            " Pantente, Chofer_Rut, Modelo, Year, Capacidad " +
            " FROM bus" +
            " ORDER BY Pantente ");
         ResultSet res = pstm.executeQuery();
         int i = 0;
         while(res.next()){
            String estCodigo = res.getString("Pantente");
            String estNombre = res.getString("Chofer_Rut");
            String estpaterno = res.getString("Modelo");
            String valor=res.getString("Year");
            String Cap=res.getString("Capacidad");
            data[i][0] = estCodigo;            
            data[i][1] = estNombre;            
            data[i][2] = estpaterno;    
            data[i][3]=valor;
            data[i][4]=Cap;
            i++;
         }
         res.close();
          }catch(SQLException e){
         System.out.println(e);
    }
    return data;
 }
}
